<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Model;

use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model as MineModel;

/**
 * @property string $id 预约订单编号
 * @property string $user_id 用户id
 * @property int $info_id 预约活动信息id
 * @property \Carbon\Carbon $booking_day 预约日期
 * @property string $booking_time 预约时间段
 * @property int $status 预约状态1已预约2已取消3已使用
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 */
class AttractionUserBooking extends MineModel
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'attraction_user_booking';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['user_id', 'info_id', 'booking_day', 'booking_time', 'status'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = [
        'id' => 'integer',
        'user_id' => 'integer',
        'info_id' => 'integer',
        'status' => 'integer',
        'booking_day' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime'
    ];

    public function user()
    {
        return $this->hasOne(AttractionUser::class, 'id', 'userId');
    }

    public function tourist()
    {
        return $this->belongsToMany(AttractionUserTouristInfo::class, 'attraction_user_tourist_booking', 'booking_id', 'tourist_id')
            ->select('id', 'name', 'phone', 'id_card as idCard');
    }

    public function info()
    {
        return $this->hasOne(AttractionInfo::class, 'id', 'infoId');
    }

    public function toArray(): array
    {
        $data = parent::toArray();
        $data['bookingDay'] = date('Y-m-d', strtotime($data['bookingDay']));
        return $data;
    }
}